package org.hive.data
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	import org.hive.managers.plugin.SoundManager;

	public class SoundData extends Sound
	{
		public var soundTransform 		:SoundTransform = new SoundTransform();
		public var soundChannel 		:SoundChannel = new SoundChannel();
		
		public var positionInLoadSet 	:Number = 0;
		public var incrementPassed		:Boolean;
		public var isLoading 			:Boolean = false;
		public var isLoaded				:Boolean = false;
		
	 	private var __name 				:String;
	 	private var __URL 				:String;
		private var __URLRequest		:URLRequest;
		
		public function SoundData( name:String="", URL:String=null )
		{
			init(name, URL);
		}
		
		public function init( name:String="", URL:String=null ):void
		{
			__name = name;
			__URL = URL;
			__URLRequest = new URLRequest( URL );
		}
		
		public var loops				:Number = 0;
		
		public function playSound(startTime:Number = 0, sndTransform:SoundTransform = null):void
		{
			if( SoundManager.hasAudio ){
				soundChannel = play(startTime, loops, sndTransform);
				soundChannel.soundTransform = soundTransform;
				soundChannel.addEventListener( Event.SOUND_COMPLETE, soundComplete, false, 0, true );
			}
		}
		
		private function soundComplete( e:Event ):void
		{
			soundChannel.removeEventListener( Event.SOUND_COMPLETE, soundComplete );
			dispatchEvent( new Event( Event.SOUND_COMPLETE ) );
		}
		
		public function stopSound():void
		{
			if( SoundManager.hasAudio ) soundChannel.stop();
		}
		
		public function set volume( num:Number ):void
		{
			soundTransform.volume = num;
			if( SoundManager.hasAudio ) soundChannel.soundTransform = soundTransform;
		}
		
		//DATA
		public function get URL():URLRequest
		{
			return __URLRequest;
		}
		
		public function get name():String
		{
			return __name;
		}
	}
}